//package com.gxd.ssoclient.filter;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.gxd.commons.model.Result;
//import com.gxd.ssoclient.entity.JwtToken;
//import com.gxd.ssoclient.utils.JwtHelper;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.web.context.support.SpringBeanAutowiringSupport;
//
//import javax.servlet.*;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * @Author:gxd
// * @Description:
// * @Date: 20:15 2018/2/22
// * @Modified By:
// */
//public class ValidateTokenFilter implements Filter {
//    @Value("${jwt.base64Security}")
//    private String base64Security;
//    // token类型，验签用
//    public final String tokenType = "bearer";
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        // TODO Auto-generated method stub
//        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, filterConfig.getServletContext());
//
//    }
//
//    /**
//     * 验签过滤器
//     */
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        // TODO Auto-generated method stub
//
//        HttpServletRequest httpRequest = (HttpServletRequest) request;
//        String auth = httpRequest.getHeader("Authorization");
//        System.out.println(tokenType.length());
//        if ((auth != null) && (auth.length() > tokenType.length())) {
//            String HeadStr = auth.substring(0, tokenType.length()).toLowerCase();
//            if (HeadStr.compareTo(tokenType) == 0) {
//                auth = auth.substring(tokenType.length(), auth.length());
//                JwtToken jwtToken = JwtHelper.parseJWT(auth,base64Security,true);
//                if (jwtToken != null) {
//                    request.setAttribute("userId", jwtToken.getUserId());
//                    request.setAttribute("accountNum", jwtToken.getAccountNum());
//                    request.setAttribute("deviceNumber", jwtToken.getDeviceNumber());
//                    request.setAttribute("device", jwtToken.getDevice());
//                    request.setAttribute("version", jwtToken.getVersion());
//                    chain.doFilter(request, response);
//                    return;
//                }
//            }
//        }
//        HttpServletResponse httpResponse = (HttpServletResponse) response;
//        httpResponse.setCharacterEncoding("UTF-8");
//        httpResponse.setContentType("application/json; charset=utf-8");
//        httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//
//        ObjectMapper mapper = new ObjectMapper();
//        Result result = new Result();
//        result.setState(-1);
//        result.setMessage("验证失败");
//        httpResponse.getWriter().write(mapper.writeValueAsString(result));
//        return;
//    }
//
//    @Override
//    public void destroy() {
//        // TODO Auto-generated method stub
//
//    }
//}
